//
//  NSString+MailString.m
//  LJEmailTextField
//
//  Created by liujinlong on 15/3/13.
//  Copyright (c) 2015年 com.ljl. All rights reserved.
//

#import "NSString+MailString.h"

@implementation NSString (MailString)

- (BOOL)showMailList
{
    NSRange range = [self rangeOfString:@"@"];
    
    if (range.location != NSNotFound && range.location != 0) {
        return YES;
    }
    
    return NO;
}

- (NSArray *)matchMailListWithMailList:(NSArray *)mailList
{
    NSMutableArray * mutMailList = [[NSMutableArray alloc] init];
    
    if ([self rangeOfString:@"@"].location == self.length - 1) {
        
        for (NSString * mailSuffix in mailList) {
            NSString * mailString = [NSString stringWithFormat:@"%@%@", self, mailSuffix];
            [mutMailList addObject:mailString];
        }
    }
    else
    {
        //按“@“将输入的字符串进行分割
        NSArray * strParts = [self componentsSeparatedByString:@"@"];
        
        //如果分割的部分大于2，说明有多个@，非法邮箱
        if (strParts.count > 2) {
            return [mutMailList copy];
        }
        
        for (NSString * mailSuffix in mailList) {
            if ([mailSuffix hasPrefix:strParts[1]]) {
                NSString * mailString = [NSString stringWithFormat:@"%@@%@", strParts[0], mailSuffix];
                [mutMailList addObject:mailString];
            }
        }
    }
    
    //如果最终仅有一个匹配，则邮箱列表消失
    if (mutMailList.count == 1 && [mutMailList[0] isEqualToString:self]) {
        [mutMailList removeAllObjects];
    }
    
    return [mutMailList copy];
}

@end
